package com.house365.core.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputConnection;
import android.webkit.WebView;

/**
 * WebView增加onScroll事件
 * @author gaopeng
 */
public class ObservableWebView extends WebView {
	private OnScrollListener mOnScrollListener;
	private OnCreateInputConnectionListener mOnCreateInputConnectionListener;

	public ObservableWebView(final Context context) {
		super(context);
	}

	public ObservableWebView(final Context context, final AttributeSet attrs) {
		super(context, attrs);
	}

	public ObservableWebView(final Context context, final AttributeSet attrs, final int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onScrollChanged(final int l, final int t, final int oldl, final int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		if (mOnScrollListener != null)
			mOnScrollListener.onScroll(l, t);
	}

	@Override
    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
		InputConnection conn = super.onCreateInputConnection(outAttrs);
		if (mOnCreateInputConnectionListener != null) {
			mOnCreateInputConnectionListener.onCreateInputConnection(outAttrs);
		}
	    return conn;
    }

	public OnScrollListener getOnScrollListener() {
		return mOnScrollListener;
	}

	public void setOnScrollListener(final OnScrollListener listener) {
		mOnScrollListener = listener;
	}

	public OnCreateInputConnectionListener getOnCreateInputConnectionListener() {
		return mOnCreateInputConnectionListener;
	}

	public void setOnCreateInputConnectionListener(OnCreateInputConnectionListener onCreateInputConnection) {
		this.mOnCreateInputConnectionListener = onCreateInputConnection;
	}

	public static interface OnScrollListener {
		void onScroll(int l, int t);
	}

	public static interface OnCreateInputConnectionListener {
		void onCreateInputConnection(EditorInfo outAttrs);
	}
}
